package com.huluobo.a2203parkdemo.department

import android.content.DialogInterface
import android.content.DialogInterface.OnClickListener
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2203parkdemo.R
import com.huluobo.a2203parkdemo.adapter.DepartmentAdapter
import com.huluobo.a2203parkdemo.databinding.ActivityDepartmentBinding
import com.huluobo.base.bean.DepartmentBean
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class DepartmentActivity : BaseActivity<ActivityDepartmentBinding, DepartmentViewModel>() {
    private lateinit var departmentAdapter: DepartmentAdapter
    override fun initData() {
        val map = mutableMapOf<String, Any>()
        map[Const.PARAM_DEPARTMENT_USER_ID] = 0
        val json = Gson().toJson(map)
        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
        viewModel.getAllDepartment(body)
        viewModel.getAllDepartmentSuccess.observe(this) {
            val list = Gson().fromJson(it, DepartmentBean::class.java)
            departmentAdapter.data.clear()
            departmentAdapter.data.addAll(list)
            departmentAdapter.notifyDataSetChanged()
        }

        viewModel.getDepartmentUser()
        viewModel.getUnDepartmentUser()

        viewModel.getDepartmentUserSuccess.observe(this) {
            Log.i("TagA", "已分组:$it")

        }

        viewModel.getUnDepartmentUserSuccess.observe(this) {
            Log.i("TagA", "未分组:$it")
        }

        viewModel.addDepartmentSuccess.observe(this) {
            ToastUtils.showLong("添加部门成功")
            viewModel.getAllDepartment(body)
        }
    }

    override fun initView() {
        binding.departmentHeader.headerBack.setOnClickListener { finish() }
        binding.departmentHeader.headerAdd.visibility = View.VISIBLE
        binding.departmentHeader.headerAdd.setOnClickListener {
            val view = layoutInflater.inflate(R.layout.dialog_add_department, null)
            AlertDialog.Builder(this)
                .setTitle("添加新部门")
                .setView(view)
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", object : OnClickListener {
                    override fun onClick(dialog: DialogInterface?, which: Int) {
                        val titleString = view.findViewById<EditText>(R.id.dialog_add_department_ed).text.toString()
                        val idString = view.findViewById<EditText>(R.id.dialog_add_department_userId_ed).text.toString()
                        //触发添加部门的接口
                        if (titleString.isNotEmpty() && idString.isNotEmpty()) {
                            val map = mutableMapOf<String, Any>()
                            map[Const.PARAM_DEPARTMENT_USER_ID] = idString
                            map[Const.PARAM_DEPARTMENT_NAME] = titleString
                            val json = Gson().toJson(map)
                            val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                            viewModel.addDepartment(body)
                        } else {
                            ToastUtils.showLong("id或者部门名不能为空")
                        }
                    }
                })
                .show()
        }

        binding.departmentRv.layoutManager = GridLayoutManager(this, 2)
        departmentAdapter = DepartmentAdapter()
        binding.departmentRv.adapter = departmentAdapter
    }
}